package gob.osinerg.emergencia.util;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import javax.swing.text.html.Option;

import org.apache.commons.beanutils.ConversionException;
/**
 * User: tcdata
 * Date: 29/07/2003
 * Time: 06:37:24 PM
 */
public class Converter {
    public static final String DEFAULT_DATE_PATTERN = "dd/MM/yyyy";
    public static final String DEFAULT_DATE_TIMESTAMP_PATTERN = "dd/MM/yyyy";

    public static String dateToString(java.util.Date date) throws ConversionException {
        return dateToString(date, Converter.DEFAULT_DATE_PATTERN);
    }

    public static String dateToString(java.util.Date date, String pattern) throws ConversionException {
        String result = null;
        if (date == null) {
            throw new ConversionException("Parameter date is null");
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        result = formatter.format(date);
        return result;
    }

    public static java.util.Date stringToDate(String dateStr) throws ConversionException {
        return stringToDate(dateStr, Converter.DEFAULT_DATE_PATTERN);
    }

    public static java.util.Date stringToDate(String dateStr, String pattern) throws ConversionException {
        java.util.Date date = null;
        if (dateStr == null || dateStr.trim().equals("")) {
            throw new ConversionException("Invalid value for parameter dateStr");
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        try {
            date = formatter.parse(dateStr);
        }
        catch (ParseException pe) {
            throw new ConversionException(pe);
        }
        return date;
    }

    public static java.sql.Date stringToSqlDate(String dateStr) throws ConversionException {
        java.sql.Date sqlDate = null;
        java.util.Date date = stringToDate(dateStr);
        sqlDate = new java.sql.Date(date.getTime());
        return sqlDate;
    }

    public static java.sql.Date stringToSqlDate(String dateStr, String pattern) throws ConversionException {
        java.sql.Date sqlDate = null;
        java.util.Date date = stringToDate(dateStr, pattern);
        sqlDate = new java.sql.Date(date.getTime());
        return sqlDate;
    }
    
    public static Vector getFechas() {
        
     Vector lista  = new Vector();
        
      Timestamp fechaActualT = new Timestamp(System.currentTimeMillis());
      Timestamp fechaInicioT = new Timestamp(System.currentTimeMillis());

       long lFechaFin = fechaActualT.getTime();

        long milisDia = 24*60*60*1000;
        long numDias = 30;
        long fechaInicio = lFechaFin - numDias*milisDia;
             
        fechaInicioT.setTime(fechaInicio);
                
         String s_fecha_inicio = fechaInicioT.toString();
         String s_fecha_fin = fechaActualT.toString();
             
         Date fecha1 = new Date(fechaInicio);
         Date fecha2 = new Date(lFechaFin);
             
         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
         String strFecha1 = sdf.format(fecha1);
         String strFecha2 = sdf.format(fecha2); 
             
         lista.addElement(strFecha1);
         lista.addElement(strFecha2);
        
         return lista;
        
    }
    
      public  Timestamp stringToSqlTimestamp(String strDate) throws ConversionException {
        java.util.Date date = stringToDate(strDate, Converter.DEFAULT_DATE_TIMESTAMP_PATTERN);
        Timestamp timestamp = new Timestamp(date.getTime());
        return timestamp;
    }
    public String procesoCalcularDia(String strDate) throws ConversionException { 
        String numDiaCalculado = "";
        java.util.Date date = stringToDate(strDate, Converter.DEFAULT_DATE_TIMESTAMP_PATTERN);
        Timestamp timestamp = new Timestamp(date.getTime());
        long lFechaFin = timestamp.getTime();
        Timestamp fechaActual = new Timestamp(System.currentTimeMillis());
        long fechaActualT = fechaActual.getTime();
        long milisDia = 24*60*60*1000;
        long numDias = 0;
        numDias  = ( fechaActualT -  lFechaFin)/(milisDia);
        numDiaCalculado = Long.toString(numDias);
        return numDiaCalculado;
    }
    
    public static ArrayList getListaMeses(){
    	   
        ArrayList list = new ArrayList();
       
        String []meses={"ENERO","FEBRERO","MARZO","ABRIL","MAYO","JUNIO","JULIO",
                        "AGOSTO","SEPTIEMBRE","OCTUBRE","NOVIEMBRE","DICIEMBRE"};
                        
         for (int i=0;i<meses.length;i++){
        	 Map map = new HashMap();
        	 map.put("id", new Integer(i + 1));
        	 map.put("descripcion", meses[i]);

        	 list.add(map);
         }      
       
        return list;
       }   
   
	public static ArrayList getListaAnnos() {
		ArrayList list = new ArrayList();
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		for (int i = 2014; i <= year; i++) {
		//for (int i = 1998; i <= year; i++) {
			Map map = new HashMap();
			map.put("id", new Integer(i));
			map.put("descripcion", String.valueOf(i));
			list.add(map);
		}
		return list;
	}
}
